<template>
  <work-template @submitOn="submitOn" v-model="ruleForm"
                 :flowId="flowId"
                 @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form" class="container-div">
      <!--车辆信息  订单信息-->
      <vehicleMessage v-model="ruleForm" :state="isEdit" ref="refVehicleMessage"/>
      <!--申请信息-->
      <applyForMessage v-model="ruleForm" :state="isEdit" ref="refApplyForMessage"/>
    </div>

  </work-template>
</template>

<script>
import {carLeasePurchaseTransfer, getDetails, submit} from "@/api/vehicleDisposal/purchaseHandle.js"
import {ScrapHandleNew} from "@/api/vehicleDisposal/scrapHandle";

export default {
  name: 'PurchaseHandleNew',
  components: {
    vehicleMessage: () => import('../../components/purchaseHandle/vehicleMessage.vue'),
    applyForMessage: () => import('../../components/purchaseHandle/applyForMessage.vue'),
  },
  computed: {
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      return this.$route.query?.flowId
    }
  },
  watch: {},
  data() {
    return {
      ruleForm: {
        carId: '',
        carNo: '',
        vinNo: '',
        brandId: '',
        seriesId: '',
        modelId: '',
        brandName: '',
        modelName: '',
        seriesName: '',
        propertyRightId: '',
        propertyRightName: '',
        operationsName: '',
        operationsId: '',
        ascriptionId: '',
        ascription: '',
        outStorageTime: '',
        returnTime: '',
        financialPassedTime: '',
        orderNo: '',

        customerName: '',
        customerPhone: '',
        primaryContact: '',
        primaryContactPhone: '',
        primaryContactIdentity: '',
        secondaryContact: '',
        secondaryContactPhone: '',
        secondaryContactWorkDuties: '',
        schemeName: ''
      }
    };
  },
  created() {
    this.handleDetail()
  },
  mounted() {
  },
  methods: {
    async handleDetail() {
      this.loading = true
      try {
        if (!this.$route.query.id) return
        const res = await getDetails(this.$route.query.id)
        this.ruleForm = res.data
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    async submitOn() {
      let loading = null
      try {
        await Promise.all([
          this.$refs.refVehicleMessage.handleEffect(),
          this.$refs.refApplyForMessage.handleEffect(),
        ]);

        loading = this.$loading({
          lock: true,
          text: '保存中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });

        const apiToCall = !this.$route.query?.id ? carLeasePurchaseTransfer : submit;
        const res = await apiToCall(this.ruleForm);

        if (res.code === 200) {
          this.$message.success('保存成功');
          this.closeSelectedTag();
        }
      } catch (error) {
        console.error('保存过程中出现错误:', error);
      } finally {
        loading?.close();
      }
    },


    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/VehicleDisposal/RentPurchaseTransfer/PurchaseHandleList",
        });
      });
    }
  },
};
</script>

<style scoped lang=""></style>


